//
//  MCBankcardManageCell.m
//  TLYL
//
//  Created by MC on 2017/8/14.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCBankcardManageCell.h"
#import "MCDataTool.h"
#define HeightCard  95
@implementation MCSingleGestureRecognizer
@end
@interface MCBankcardManageView ()

/*
 * 打底
 */
@property (nonatomic,strong)UIImageView * backView;

/*
 * 图标
 */
@property (nonatomic,strong)UIImageView * logoImgV;
/*
 * 卡的名称
 */
@property (nonatomic,strong)UILabel * cardNameLab;

/*
 * 卡号
 */
@property (nonatomic,strong)UILabel * cardNumberLab;


@end

@implementation MCBankcardManageView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}



-(void)createUI{
    self.backgroundColor=[UIColor clearColor];
    
    
    _backView=[[UIImageView alloc]init];
    [self addSubview:_backView];
    _backView.frame=CGRectMake(0, 0, G_SCREENWIDTH, 115);
    
    
    /*
     * logo
     */
    _logoImgV=[[UIImageView alloc]init];
    [_backView addSubview:_logoImgV];
    _logoImgV.backgroundColor=[UIColor whiteColor];
    _logoImgV.layer.cornerRadius=12.5;
    _logoImgV.clipsToBounds=YES;
    _logoImgV.frame=CGRectMake(10+15, 25+10, 25, 25);
    
    
    /*
     * 卡的名称
     */
    _cardNameLab =[[UILabel alloc]initWithFrame:CGRectZero];
    _cardNameLab.textColor=[UIColor whiteColor];
    _cardNameLab.font=[UIFont boldSystemFontOfSize:18];
    _cardNameLab.text =@"加载中...";
    _cardNameLab.textAlignment=NSTextAlignmentLeft;
    [_backView  addSubview:_cardNameLab];
    
  
    
    
    
    /*
     * 卡号
     */
    _cardNumberLab =[[UILabel alloc]initWithFrame:CGRectZero];
    _cardNumberLab.textColor=[UIColor whiteColor];
    _cardNumberLab.font=[UIFont boldSystemFontOfSize:18];
    _cardNumberLab.text =@"加载中...";
    _cardNumberLab.textAlignment=NSTextAlignmentLeft;
    [_backView  addSubview:_cardNumberLab];
    
    
    
    [self relayOutConstraints];

}

-(void)relayOutConstraints{
    
    
    
    [_cardNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_logoImgV.mas_centerY);
        make.width.mas_equalTo(G_SCREENWIDTH-90);
        make.left.equalTo(_logoImgV.mas_right).offset(10);
    }];
    
    [_cardNumberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_cardNameLab.mas_bottom).offset(13);
        make.width.mas_equalTo(G_SCREENWIDTH-90);
        make.left.equalTo(_logoImgV.mas_right).offset(10);
    }];
    
    
    
    
    
}

-(void)setDataSource:(MCBankModel *)dataSource{
    
    _dataSource=dataSource;
    _cardNumberLab.text=dataSource.showBankNumber;
    
    
    //默认银行卡
    if ([dataSource.Isdefault intValue]==1) {
        _cardNameLab.text=[NSString stringWithFormat:@"%@  (默认此卡)",[MCDataTool MC_GetDic_Bank][dataSource.BankCode][@"name"]];
        
    }else{
        _cardNameLab.text =[MCDataTool MC_GetDic_Bank][dataSource.BankCode][@"name"];
        
    }
    
    _logoImgV.image=[UIImage imageNamed:dataSource.BankCode];
    

    /*
     * 设置开奖号的富文本
     */
    NSRange range_lotteryNumber = [_cardNameLab.text rangeOfString:@"(默认此卡)"];
    [self setTextColor:_cardNameLab FontNumber:[UIFont boldSystemFontOfSize:12] AndRange:range_lotteryNumber AndColor:[UIColor whiteColor]];

}

//设置不同字体颜色
-(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    label.attributedText = str;
}


-(void)setBack:(BOOL)isSignal{
    if (isSignal) {
        _backView.image=[UIImage imageNamed:@"BankBack2"];
    }else{
        _backView.image=[UIImage imageNamed:@"BankBack1"];
    }
}

+(CGFloat)computeHeight:(id)info{
    
    return HeightCard;
    
}

@end

@interface MCBankcardManageCell ()



@end

@implementation MCBankcardManageCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.backgroundColor=[UIColor clearColor];

}

-(void)setDataSource:(NSMutableArray<MCBankModel *> *)dataSource{
    _dataSource=dataSource;
    int i=0;
    for (MCBankModel *model in dataSource) {
    
        MCBankcardManageView * view =[self viewWithTag:i+12345];
        if (view) {
            
        }else{
            view=[[MCBankcardManageView alloc]init];
            [self addSubview:view];
        }
        
        view.dataSource=model;
        [self setBankcardManageView:view andIndex:i];
        i++;
        if (i%2==0) {
            [view setBack:YES];
        }else{
            [view setBack:NO];
        }
        
    }
}

-(void)setBankcardManageView:(MCBankcardManageView *)view andIndex:(int)index{
    
    view.frame=CGRectMake(0, HeightCard*index, G_SCREENWIDTH, HeightCard);
    view.tag=12345+index;
    MCSingleGestureRecognizer* singleRecognizer = [[MCSingleGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    singleRecognizer.tag=index;
    singleRecognizer.model=_dataSource[index];
    [view addGestureRecognizer:singleRecognizer];
    
}
-(void)SingleTap:(MCSingleGestureRecognizer*)recognizer
{
    //处理单击操作
    MCBankModel *model =recognizer.model;
    if ([self.delegate respondsToSelector:@selector(setDefaultBankCard:)]) {
        [self.delegate setDefaultBankCard:model];
    }
    
    
}

+(CGFloat)computeHeight:(int)info{
    
    return HeightCard*info;
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

